<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>添加备件</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../../../css/global.css">
		<link rel="stylesheet" href="../../../../../css/style.css">
		<link rel="stylesheet" href="../../../../../css/mui.picker.css">
		<link rel="stylesheet" href="../../../../../css/mui.poppicker.css">
		<style>
			.mui-btn {
				font-size: 16px;
				margin-bottom: 0;
				border: 0;
			}
			h5.mui-content-padded {
				margin-left: 3px;
				margin-top: 20px !important;
			}
			h5.mui-content-padded:first-child {
				margin-top: 12px !important;
			}
			.ui-alert {
				text-align: center;
				padding: 20px 10px;
				font-size: 16px;
			}
			input.d_ipt{
				padding: 0;
			}

		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">添加备件</h1>
		</header>
		<div class="mui-content">
			<div class="details_con">
				<div hidden>
					<input type="hidden" id="isWarranty" />
					<input type="hidden" id="helpId" />
					<input type="hidden" id="phoneHelpId" />
					<!-- 备件类型 -->
					<input type="hidden" id="portType" />
					<input type="hidden" id="loanNumber" />
					<!-- 备件编号 -->
					<input type="hidden" id="pid" />
					<input type="hidden" id="picsize" />
				</div>
				<div class="clum">
					<span class="name">服务编号</span>
					<span id="serviceNo" class="txt"></span>
				</div>
				<div class="clum">
					<span class="name">备件号<em class="t_red">*</em></span>
					<input type="text" id="partNumber" maxlength="50" class="d_ipt" readonly="readonly" placeholder="请选择备件"/>
				</div>
				<div class="clum">
					<span class="name">备件数量<em class="t_red">*</em></span>
					<input type="number" id="qtyCount" min="0" max="" class="d_ipt" placeholder="请输入备件数量" onkeyup="qtyCountChange()" value="1"/>
				</div>
				<div class="clum">
					<span class="name">备件名称<em class="t_red">*</em></span>
					<input type="text" id="partName" maxlength="250" class="d_ipt" readonly="readonly" />
				</div>
				<div class="clum">
					<span class="name">备件类型<em class="t_red">*</em></span>
					<input type="hidden" id="partType" />
					<span class="txt">
						<button id='partTypeName' class="mui-btn mui-btn-block mui-btn-select" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum">
					<span class="name">备件序列号<em class="t_red">&nbsp;</em></span>
					<input type="text" id="partSerialNumber" maxlength="50" class="d_ipt" placeholder="请输入备件序列号"/>
				</div>
				<div class="clum">
					<span class="name">坏件序列号<em class="t_red">&nbsp;</em></span>
					<input type="text" id="damageSerialNumber" maxlength="100" class="d_ipt" placeholder="请输入坏件序列号" />
				</div>
				<div class="btn_box">
					<span id="picList" class="fl">图片信息列表</span>
					<span id="addPic" class="fr red">添加坏件图片</span>
				</div>
				<div style="height: 50px;"></div>
				<div class="line"></div>
				<div class="clum">
					<span class="name">Price单价<!-- <em class="t_red">*</em> --></span>
					<input type="number" id="price" class="d_ipt" placeholder="请输入Price单价" onkeyup="priceChange()"/>
				</div>
				<div style="margin: 15px;text-align: right;">
					<button type="button" onclick="selectPrice()" class="mui-btn mui-btn-blue">查询单价</button>
				</div>
				<div class="line"></div>
				<div class="clum">
					<span class="name">Price总价<em class="t_red">&nbsp;</em></span>
					<input type="number" id="totalprice" class="d_ipt" placeholder="请输入Price总价" value="0" readonly="readonly" />
				</div>
				<div class="clum autoHeight">
					<span class="name">备注<em class="t_red">&nbsp;</em></span>
					<div class="clear"></div>
					<textarea id="remark" class="textarea" maxlength="500" placeholder="请输入备注"></textarea>
				</div>
				<div class="clum">
					<span class="name">坏件是否还库<em class="t_red">*</em></span>
					<input type="hidden" id="isOutWarranty" value="0"/>
					<span class="txt">
						<button id='isOutWarrantyName' class="mui-btn mui-btn-block mui-btn-select" type='button'>否</button>
					</span>
				</div>
				<div></div>
				<!--<div class="clum autoHeight">
					<span class="name">备件状态变更说明<em class="t_red">*</em></span>
					<span class="txt">
						<button class="mui-btn mui-btn-block mui-btn-select" type='button'>否</button>
					</span>
					<div class="clear"></div>
					<textarea id="statusChange" class="textarea" maxlength="50" placeholder="请输入备件状态变更说明"></textarea>
				</div>-->
				<div class="clum">
					<span class="name">备件状态变更说明<em class="t_red">*</em></span>
					<input type="hidden" id="statusChange"/>
					<span class="txt">
						<button id='statusChangeName' class="mui-btn mui-btn-block mui-btn-select" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum autoHeight" id="changeRemarks">
					<!-- <span class="name">备件状态变更说明<em class="t_red">*</em></span> -->
					<div class="clear"></div>
					<textarea id="changeRemark" class="textarea" placeholder="请输入备件状态变更说明"></textarea>
				</div>
				<div class="btn_box">
					<span id="cancale" class="fl">取消</span>
					<span id="saveBtn" class="fr red">保存</span>
				</div>
				<div style="height: 50px;"></div>
			</div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../../../js/mui.min.js"></script>
		<script src="../../../../../js/utils/h.js"></script>
		<script src="../../../../../js/constant.js"></script>
		<script src="../../../../../js/common.js"></script>
		<script src="../../../../../js/utils/base64.js"></script>
		<script src="../../../../../js/utils/md5.js"></script>
		<script src="../../../../../js/utils/sortMap.js"></script>
		<script src="../../../../../js/utils/http.js"></script>
		<script src="../../../../../js/app.js"></script>
		<!-- 基础js end -->
		<script src="../../../../../js/utils/rolldate.min.js"></script>
		<script src="../../../../../js/mui.picker.js"></script>
		<script src="../../../../../js/mui.poppicker.js"></script>
		<script src="../../../../../js/fse/service/report/phone/spareparts/spareparts.js"></script>
		<script type="text/javascript" charset="utf-8">
			/*
				
			*/
			var PicPartsList; //图片清单
			var SNPartsList; //序列号清单
			(function($, doc) {
				$.init();

				$.plusReady(function() {
					h('#statusChange').parent().hide()
					h('#changeRemarks').hide()
					var self = plus.webview.currentWebview();
					// 获取上一个页面传过来的参数并显示到页面
					h('#serviceNo').html(self.serviceNo);
					h('#phoneHelpId').val(self.phoneHelpId);
					h('#isWarranty').val(self.isWarranty)
					h('#portType').val(self.type)
					getPicPartsList();//查询图片清单
					getSNPartsList();//查询序列号清单


					if (self.isWarranty != '保外') {
						h('#isOutWarranty').parent().hide()
					}
					
					var helpId = new Date().UUID(self.serviceNo);
					h('#helpId').val(helpId);
					
					// 选择备件
					h('#partNumber').tap(function() {
						app.hideSoftKeybord()
						app.jumpTo('select.html', 'view/service/report/fieldservice/spareparts/select.html', {
							serviceNo: self.serviceNo,
							phoneHelpId: self.phoneHelpId
						});
					});

					// 备件类型
					h('#partTypeName').tap(function() {
						var userPicker = new $.PopPicker();
						// [{"text":"更换","value":"1"},{"text":"漏发","value":"2"},{"text":"错发","value":"3"},{"text":"多发","value":"4"},{"text":"个人工具","value":"5"}]
						ajaxGetJson(BASE_URL + 'cc/dict/selectDict', {
							dictType: 'parttype_baogao'
						}, function(data) {
							console.log(JSON.stringify(data))
							if (!data) {
								app.toast("无备件类型数据")
								return
							}
							userPicker.setData(data);
							userPicker.show(function(items) {
								h('#partTypeName').html(items[0].text)
								h('#partType').val(items[0].value)
								userPicker.dispose()
							});
						});
					});
					
					// 坏件是否还库
					h('#isOutWarrantyName').tap(function() {
						var userPicker = new $.PopPicker();
						userPicker.setData([{value: '0', text: '否'},{value: '1', text: '是'}]);
						userPicker.show(function(items) {
							h('#isOutWarrantyName').html(items[0].text)
							h('#isOutWarranty').val(items[0].value)
							if (items[0].value == "1") {
								h('#statusChange').parent().show()
							} else {
								h('#statusChange').parent().hide()
							}
							userPicker.dispose()
						});
					});

					// 备件状态变更说明
					h('#statusChangeName').tap(function() {
						app.hideSoftKeybord()
						var userPicker = new $.PopPicker();

						ajaxGetJson(BASE_URL + 'cc/dict/selectCCDict', {
							tableName: 'StatusChange',
							engineerId: app.getUser().enginnerId
						}, function(data) {
							console.log(JSON.stringify(data))
							if (!data) {
								app.toast("无备件状态变更说明数据")
								return
							}
							userPicker.setData(data);
							userPicker.show(function(items) {
								h('#statusChangeName').html(items[0].text)
								h('#statusChange').val(items[0].value)
								if (items[0].value == "4") {
									h('#changeRemarks').show()
								} else {
									h('#changeRemarks').hide()
								}
								userPicker.dispose()
							});
						});
					});

					// 图片信息列表
					h('#picList').tap(function() {
						app.jumpTo('../../../picture/list.html', 'view/service/picture/list.html', {
							helpId: h('#helpId').val(),
							serviceNo: h('#serviceNo').html()
						});
					});

					// 添加坏件图片
					h('#addPic').tap(function() {
						app.jumpTo('../../../picture/add.html', 'view/service/picture/add.html', {
							helpId: h('#helpId').val(),
							serviceNo: h('#serviceNo').html()
						});
					});

					// 保存备件信息
					h('#saveBtn').tap(function() {
						app.hideSoftKeybord()
						addSpareparts()
					})

					// 取消
					h('#cancale').tap(function() {
						$.back()
						// app.jumpTo('view/service/report/fieldservice/spareparts/select.html', 'view/service/report/fieldservice/spareparts/select.html');
					})

				});
			}(mui, document));

			function addPictureSuccess(size) {
				console.log("图片添加成功")
				h('#picsize').val(size)
			}
			
			function updatePictureSize(size) {
				h('#picsize').val(size)
			}
			
			function qtyCountChange() {
				var qtyCount = h('#qtyCount').val();
				// var qtyCountMax = h('#qtyCount').attr('max');
				// var qtyCountMin = h('#qtyCount').attr('min');
				// if (qtyCount < qtyCountMin ) {
				// 	qtyCount = qtyCountMin;
				// 	h('#qtyCount').val(qtyCount)
				// }
				// if (qtyCount > qtyCountMax ) {
				// 	qtyCount = qtyCountMax;
				// 	h('#qtyCount').val(qtyCountMax)
				// }
				var price = h('#price').val() || 0;
				h('#totalprice').val((qtyCount * price)) 
			}
			
			function priceChange() {
				var qtyCount = h('#qtyCount').val();
				var price = h('#price').val() || 0;
				h('#totalprice').val((qtyCount * price));
			}
			
			function selectPrice() {
				var partCode = h('#partNumber').val();
				if (!partCode) {
					app.toast("请选择备件")
					return;
				}
				app.showWaiting("查询中...")
				ajaxGet(BASE_URL + 'cc/report/port/selectPriceByPartcode', {
					partCode
				}, function(data) {
					app.closeWaiting();
					if (data.code === 200) {
						app.toast("查询成功");
						var qtyCount = h('#qtyCount').val();
						var price = data.data.price;
						h('#price').val(price) 
						h('#totalprice').val((qtyCount * price))
					} else {
						app.toast(data.msg)
					}
				}, function(errorMsg) {
					app.closeWaiting();
					app.toast(errorMsg)
				}) 
			}
			
			function selectPartCallback(partId, partNo, partName, partSerialNumber, partQty, loanNumber) {
				console.log(partNo)
				h('#pid').val(partId);
				h('#partNumber').val(partNo);
				h('#partName').val(partName);
				// h('#partSerialNumber').val(partSerialNumber);
				h('#qtyCount').attr('min', '1');
				h('#qtyCount').attr('max', partQty);
				h('#qtyCount').val(partQty);
				h('#loanNumber').val(loanNumber);
				
				// var qtyCount = h('#qtyCount').val();
				// var price = h('#price').val() || 0;
				// h('#totalprice').val((qtyCount * price)); 
				h('#partType').val("");
				h('#partTypeName').html("请选择");
				h('#partSerialNumber').val("");
				h('#damageSerialNumber').val("");
				h('#price').val("");
				h('#totalprice').val(0);
				h('#remark').val("");
				h('#isOutWarranty').val("0");
				h('#isOutWarrantyName').html("否");
				h('#statusChange').val("");
				h('#statusChangeName').html("请选择");
				h('#changeRemark').val("");	
			}
		</script>
		<!-- js代码 end -->
	</body>
</html>
